package com.ys.util.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.bo.util.StringUtil;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.sys.YsCol;
import com.ys.entity.sys.YsDict;
import com.ys.entity.sys.YsGridCol;
import com.ys.enume.sys.YsGridColEnum;

public class SubGridUtil {
	
	/*
	 * 获取bootstrap table的columns
	 */
	public static List<SubGridConfigDto.ColGridDto> listColumn(List<YsGridCol> listYsGridCol
			) {
		List<SubGridConfigDto.ColGridDto> colGridArr = new ArrayList<SubGridConfigDto.ColGridDto>();
		
		// 选中框
		{
			Long id = null;
			String title = null;
			String field = null;
			String type = "checkbox";
			String totalRowText = null;
			boolean totalRow = false;
			boolean hide = false;
			String width = "50";
			String align = null;
			String modalRelativeCode = null;
			String modalValueCode = null;
			String dictKeyBase = null;
			String modalKey = null;
			String templet = null;
			Integer stateEdit = null;
			Integer multyFileSourceType = null;
			Integer isRowspan = 0;

			colGridArr.add(new SubGridConfigDto.ColGridDto(id, title, field, type, totalRowText, totalRow, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,isRowspan));
		}


		// 序号列
		{
			Long id = null;
			String title = "序号";
			String field = null;
			String type = "numbers";
			String totalRowText = null;
			boolean totalRow = false;
			boolean hide = false;
			String width = "50";
			String align = null;
			String modalRelativeCode = null;
			String modalValueCode = null;
			String dictKeyBase = null;
			String modalKey = null;
			String templet = null;
			Integer stateEdit = null;
			Integer multyFileSourceType = null;
			Integer isRowspan = 0;

			colGridArr.add(new SubGridConfigDto.ColGridDto(id, title, field, type, totalRowText, totalRow, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,isRowspan));
		}
		
		for (YsGridCol ysGridCol : listYsGridCol) {

			Long id = ysGridCol.getId();
			String title = ysGridCol.getName();
			String field = StringUtil.firstCharacterToLower(ysGridCol.getCode());
			String totalRowText = null;
			boolean totalRow = YsGridColEnum.TotalRow.getLayerValue(ysGridCol.getTotalRow());
			String type = "normal";//ysGridCol.getFrontType();
			boolean hide = ysGridCol.getFrontShow() == YsGridColEnum.FrontShow.SHOW.value ? false : true;
			String width = "" + ysGridCol.getFrontWidth();
			String align = YsGridColEnum.FrontAlign.getLayerAlign(ysGridCol.getFrontAlign());
			String modalRelativeCode = ysGridCol.getModalRelativeCode();
			String modalValueCode = ysGridCol.getModalValueCode();
			String dictKeyBase = ysGridCol.getDictKeyBase();
			String modalKey = ysGridCol.getModalKey();
			String templet = null;
			Integer stateEdit = ysGridCol.getFrontEdit();
			Integer multyFileSourceType = ysGridCol.getMultyFileSourceType();
			Integer isRowspan = ysGridCol.getIsRowspan();
			
			if (ysGridCol.getFrontEdit() == YsGridColEnum.FrontEdit.YES.value) {

				if (YsGridColEnum.FrontType.TEXT.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.TEXT_NUMBER.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.DATE_YYMM.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.DATE_YYMMDD.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.SELECTOR_MODAL_SINGLE.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.IMG.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.FILE.value.equals(ysGridCol.getFrontType())
						|| YsGridColEnum.FrontType.FILE_MULTY.value.equals(ysGridCol.getFrontType())) {
					templet = "#" + getTempletId(ysGridCol);
				}
			} else {

				if (YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysGridCol.getFrontType())) {

					templet = "#" + getTempletId(ysGridCol);
				} else if (YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysGridCol.getFrontType())) {

					templet = "#" + getTempletId(ysGridCol);
				} else if (YsGridColEnum.FrontType.IMG.value.equals(ysGridCol.getFrontType())) {
					templet = "#" + getTempletId(ysGridCol);
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysGridCol.getFrontType())) {
					templet = "#" + getTempletId(ysGridCol);
				}
			}
			colGridArr.add(new SubGridConfigDto.ColGridDto(id, title, field, type, totalRowText, totalRow, hide, width, align, modalRelativeCode, modalValueCode, dictKeyBase, modalKey, templet, stateEdit, multyFileSourceType,isRowspan));
		}
		return colGridArr;
	}
	
	public static List<SubGridConfigDto.TempletDto> listTemplets(List<YsGridCol> listYsGridCol,
			Map<String, List<YsDict>> KEY_LISTDICT_MAP) {
		List<SubGridConfigDto.TempletDto> list = new ArrayList<SubGridConfigDto.TempletDto>();
		for (YsGridCol ysGridCol : listYsGridCol) {
			if (ysGridCol.getFrontEdit() == YsGridColEnum.FrontEdit.YES.value) {
				if (YsGridColEnum.FrontType.TEXT.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.TEXT_NUMBER.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"number\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysGridCol.getFrontType())) {
					
					if(ysGridCol.getDictKeyBase() == null || ysGridCol.getDictKeyBase().length() == 0) {
						String id = getTempletId(ysGridCol);
						String html = "{{#  layer.msg('" + ysGridCol.getCode() + "尚未设置字典'); }}";
						list.add(new SubGridConfigDto.TempletDto(id, html));
					} else {
						String id = getTempletId(ysGridCol);
						String html = 
								"<select name=\"" + ysGridCol.getCode() + "\" lay-filter=\"_input_filter_selector_on_table_in_form\">"
								+ "{{#  layui.each(" + JSON.toJSONString(KEY_LISTDICT_MAP.get(ysGridCol.getDictKeyBase())) + ", function(index, item){ }}"
								+ "{{# if(d." + ysGridCol.getCode() + " == item.dictValue){ }} <option value=\"{{ item.dictValue }}\" selected>{{ item.dictName }}</option> {{#  } else { }} <option value=\"{{ item.dictValue }}\">{{ item.dictName }}</option>  {{#  } }} ";
						html += "{{#  }); }}";
						html += "</select>";
						list.add(new SubGridConfigDto.TempletDto(id, html));
					}
				} else if (YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysGridCol.getFrontType())) {
					
					if(ysGridCol.getDictKeyBase() == null || ysGridCol.getDictKeyBase().length() == 0) {
						String id = getTempletId(ysGridCol);
						String html = "{{#  layer.msg('" + ysGridCol.getCode() + "尚未设置字典'); }}";
						list.add(new SubGridConfigDto.TempletDto(id, html));
					} else {
						
						String layText = "";
						for (YsDict ysDict : KEY_LISTDICT_MAP.get(ysGridCol.getDictKeyBase())) {
							layText += ysDict.getDictName() + "|";
						}
						
						String id = getTempletId(ysGridCol);
						String html = "<input type=\"checkbox\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" lay-skin=\"switch\" lay-text=\"" + layText + "\" lay-filter=\"_input_filter_checkbox_on_sub_grid\" {{ d." + ysGridCol.getCode() + " == '1' ? 'checked' : '' }}>";
						list.add(new SubGridConfigDto.TempletDto(id, html));
					}
				} else if (YsGridColEnum.FrontType.SELECTOR_MODAL_SINGLE.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" data-modal-key=\"" + ysGridCol.getModalKey() + "\" data-filter=\"_input_filter_modal_single_on_sub_grid\" readonly>";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.DATE_YYMM.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_date_yyyymm_on_sub_grid\">";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.DATE_YYMMDD.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_date_day_on_sub_grid\">";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.IMG.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "{{# if(d." + ysGridCol.getCode() + "){ }} <img code=\"" + ysGridCol.getCode() + "\" src=\"{{d." + ysGridCol.getCode() + "}}\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_img_on_table_in_form\"/><i code=\"" + ysGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: red;\" title=\"移除\" data-filter=\"_icon_remove_filter_img_on_table_in_form\">&#x1007;</i>{{#  } else { }}  <img code=\"" + ysGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_add.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_img_on_table_in_form\"/>   {{#  } }}";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.FILE.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "{{# if(d." + ysGridCol.getCode() + "){ }} <img code=\"" + ysGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_file.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_file_single_on_table_in_form\"/><i code=\"" + ysGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: red;\" title=\"移除\" data-filter=\"_icon_remove_filter_file_single_on_table_in_form\">&#x1007;</i><i code=\"" + ysGridCol.getCode() + "\" class=\"layui-icon\" style=\"color: blue;\" title=\"下载\" data-filter=\"_icon_download_filter_file_single_on_table_in_form\">&#xe615;</i>{{#  } else { }}  <img code=\"" + ysGridCol.getCode() + "\" src=\"https://admin.mini-ease.com/static/img/img_add.jpg\" style=\"max-height:30px; max-width: 30px;\" data-filter=\"_input_filter_file_single_on_table_in_form\"/>   {{#  } }}";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.FILE_MULTY.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
//					String html = "<input type=\"text\" class=\"layui-input ys-input-table-in-form\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" autocomplete=\"off\" data-filter=\"_input_filter_text_on_sub_grid\">";
					String html = "{{# if(d.id){ }} <input id=\"id_multy_file_" + System.currentTimeMillis() + "\" class=\"layui-input\" code=\"" + ysGridCol.getCode() + "\"  data-filter=\"_input_filter_file_multy_on_table_in_form\" data-source-code=\"" + ysGridCol.getCode() + "\" data-source-type=\"" + ysGridCol.getMultyFileSourceType() + "\" data-source-id=\"{{d.id}}\" value=\"{{d." + ysGridCol.getCode() + "}}\" readonly=\"readonly\" />{{#  } else { }}  <span>保存后可上传</span>   {{#  } }}";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = ysGridCol.getTplScript();
					list.add(new SubGridConfigDto.TempletDto(id, html));
				}
			} else {

				if (YsGridColEnum.FrontType.SELECTOR_DROPDOWN.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = 
							"{{#  layui.each(" + JSON.toJSONString(KEY_LISTDICT_MAP.get(ysGridCol.getDictKeyBase())) + ", function(index, item){ }}"
							+ "{{# if(d." + ysGridCol.getCode() + " == item.dictValue){ }} {{ item.dictName }} {{#  } }} ";
					html += "{{#  }); }}";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELECTOR_SWITCH.value.equals(ysGridCol.getFrontType())) {
					
					String layText = "";
					for (YsDict ysDict : KEY_LISTDICT_MAP.get(ysGridCol.getDictKeyBase())) {
						layText += ysDict.getDictName() + "|";
					}
					
					String id = getTempletId(ysGridCol);
					String html = "<input type=\"checkbox\" name=\"" + ysGridCol.getCode() + "\" value=\"{{d." + ysGridCol.getCode() + "}}\" lay-skin=\"switch\" lay-text=\"" + layText + "\" lay-filter=\"_input_filter_checkbox_on_table_in_form\" {{# if(d." + ysGridCol.getCode() + " === '1'){ }} checked {{#  } }} disabled>";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.IMG.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = "{{# if(d." + ysGridCol.getCode() + "){ }} <img src=\"{{d." + ysGridCol.getCode() + "}}\" style=\"max-height:30px; max-width: 30px;\"/>{{#  } }}";
					list.add(new SubGridConfigDto.TempletDto(id, html));
				} else if (YsGridColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysGridCol.getFrontType())) {
					String id = getTempletId(ysGridCol);
					String html = ysGridCol.getTplScript();
					list.add(new SubGridConfigDto.TempletDto(id, html));
				}
//				else if (YsColEnum.FrontType.SELF_DEFINE_TPL.value.equals(ysGridCol.getFrontType())) {
//					templet = "#" + getTempletId(ysGridCol);
//				}
			}
		}
		
		return list;
	}
	
	/*
	 * 获取templet的命名
	 * 格式：templet_script_xxxx
	 */
	public static String getTempletId(YsGridCol ysGridCol) {
		
		return "templet_script_sub_grid_" + ysGridCol.getId();
	}
	
	/*
	 * 获取表单lay-filter的命名
	 * 格式：input_filter_xxxx
	 */
	public static String getFilterNameInput(YsCol ysCol) {
		
		return "input_filter_" + ysCol.getCode();
	}
	
	/*
	 * 获取选择框lay-filter的命名
	 * 格式：select_filter_xxxx
	 */
	public static String getFilterNameSelector(YsCol ysCol) {
		
		return "select_filter_" + ysCol.getCode();
	}
	
	public static String ColGridToString(List<SubGridConfigDto.ColGridDto> colGridArr) {
		StringBuffer sb = new StringBuffer("[[");
		for (int i = 0; colGridArr != null && i < colGridArr.size(); i++) {
			SubGridConfigDto.ColGridDto colGridDto = colGridArr.get(i);
			
			if (i == 0) {
				sb.append(colGridDto.toString());
			} else {
				sb.append("," + colGridDto.toString());
			}
		}
		sb.append("]]");
		return sb.toString();
	}
}
